package com.jxc.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jxc.dao.RecordDao;
import com.jxc.entity.Record;
import com.jxc.util.PageBean;

public class RecordDaoImpl extends HibernateDaoSupport implements RecordDao {

	public Record getRecord(Record record) {
		 
		return (Record)getHibernateTemplate().get(Record.class, record.getId());
	}

	public PageBean getRecordList(final String hql,final  PageBean pb) {
		this.getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query=session.createQuery(hql);
				pb.setTotalitem(query.list().size());
				query.setFirstResult((pb.getPageon()-1)*pb.getPagesize());
				query.setMaxResults(pb.getPagesize());
				pb.getTotalpage();
				pb.setList(query.list());
				return null;
			}
		});
		return pb;
	}

	public void update(Record record) {
		getHibernateTemplate().merge(record);
	}

	public Record getRecordBySn(String sn) {
		List<Record> list=getHibernateTemplate().find("from Record record where record.sn=?",sn);
		if(list!=null&&list.size()>0)
			return list.get(0);
		return null;
	}

	public void insertRecord(Record record) {

		this.getHibernateTemplate().save(record);
	}

}
